package com.springboot_experiment_3.controller;

import com.springboot_experiment_3.model.PageVO;
import com.springboot_experiment_3.model.RegionStats;
import com.springboot_experiment_3.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author:HGF
 */
@Controller
public class testController {

    @Autowired
    TestService testService;

    @GetMapping("/")
    public String getCoronaVirusData(Model model, String countryOrRegion) throws Exception {
        List<RegionStats> regionStats = testService.getRegionStats();
        if (!StringUtils.isEmpty(countryOrRegion) && countryOrRegion.trim().length() > 0) {
            regionStats = regionStats.stream()
                    .filter(x -> x.getCountryOrRegion().toUpperCase().contains(countryOrRegion.toUpperCase()))
                    .collect(Collectors.toList());
        }
        PageVO pageVO = PageVO.builder()
                .now(new Date())
                .confirmed(testService.getConfirmed())
                .recovered(testService.getRecovered())
                .deaths(testService.getDeaths())
                .nowConfirm(testService.getNowConfirm())
                .regionStats(regionStats)
                .build();
        model.addAttribute("data", pageVO);
        return "echarts";
    }

//    @ResponseBody
//    @GetMapping("/json")
//    public List<RegionStats> getJson() throws Exception {
//        List<RegionStats> regionStats = testService.getRegionStats();
//
//    }
}
